package com.neu.questionnaire_system.controller;


import com.neu.questionnaire_system.beans.ResponseEntity;
import com.neu.questionnaire_system.entity.Answer;
import com.neu.questionnaire_system.service.impl.AnswerServiceImpl;
import org.apache.commons.mail.EmailException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jyh
 * @since 2022-11-8
 */
@RestController
@RequestMapping("/answer")
@CrossOrigin
public class AnswerController {
    @Autowired
    private AnswerServiceImpl answerService;

    /**
     * created by jyh 用户添加答者
     * @param map 接收到的data，json格式
     * @return 返回封装的对象
     */
    @RequestMapping(value = "/addAnswer")
    public ResponseEntity addAnswer(@RequestBody Map<String,Object> map) throws InvocationTargetException, IllegalAccessException {
        return answerService.addAnswer(map);
    }
    /**
     * created by jyh控制层接收一个包含键值对的map。用来查询答者
     * @return 返回封装状态信息和内容的对象
     */
    @RequestMapping("/showAnswer")
    public ResponseEntity showAnswer(){
        return answerService.showAnswer();
    }
    /**
     * created by jyh 当前用户删除答者
     * @param map 需要删除的答者的答者名
     * @return 返回封装状态信息和内容的对象
     */
    @RequestMapping("/deleteAnswer")
    public ResponseEntity deleteAnswer(@RequestBody Map<String,Object> map){
        return answerService.deleteAnswer(map);
    }

    /**
     * created by jyh 用户查答者，请求地址
     * @param map 传入的map包含answerName
     * @return 返回包含查询到用户信息的对象，包含userName、password、phone
     */
    @RequestMapping("/findAnswerByAnswerName")
    public ResponseEntity findAnswerByAnswerName(@RequestBody Map<String,Object> map){
        return answerService.findAnswerByAnswerName(map);
    }
    /**
     * created by jyh 更新答者信息，支持格式json
     * @param map 传入的map中包含userName、password、phone、uid
     * @return 返回信息
     */
    @RequestMapping("/updateAnswer")
    public ResponseEntity updateAnswerById(@RequestBody Map<String,Object> map){
        return answerService.updateAnswer(map);
    }

    /**
     * created by jyh 用户登录
     * @param password,answerName,qid 传入的用户实体类
     * @return 返回统一的对象
     */
    @RequestMapping("/answerLogin")
    public ResponseEntity AnswerLogin(String answerName, String password,String qid){
        return answerService.answerLogin(answerName,password,qid);
    }


    @RequestMapping("/sendAnswerEmail")
    public ResponseEntity sendAnswerEmail(@RequestBody Map<String,Object> map) throws EmailException {
        return answerService.sendAnswerEmail(map);
    }


}